package com.example.Config;

import com.example.Service.ResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    ResourcesService resourcesService;

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        //请求资源映射文件
        registry.addResourceHandler("/resource/**")
                //数据库存放url地址 方便后期维护
                .addResourceLocations("file:"+resourcesService.url());

        //解决http://localhost:8080/doc.html#/home 出现404的问题
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("errorTest")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
//    拦截器
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new LoginHandlerInterceptor())
//                //拦截
//                .addPathPatterns("/resource/**")
//                //放行
//                .excludePathPatterns("/index.html","/","/user/userInput","/user/userOut","/resource/GetUrl","/resource/files","/resource/GetUrls","/resource/Input","/doc.html");
//    }
    //视图解析，链接的跳转 设置初始页
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
    }
}
